package com.woniu.houtai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.houtai.entity.Comment;
import com.woniu.houtai.entity.Orderitem;
import com.woniu.houtai.mapper.CommentMapper;
import com.woniu.houtai.mapper.CommentVoMapper;
import com.woniu.houtai.mapper.OrderitemMapper;
import com.woniu.houtai.mapper.OrderitemVoMapper;
import com.woniu.houtai.service.CommentService;
import com.woniu.houtai.vo.CommentVo;
import com.woniu.houtai.vo.OrderitemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author firstGroup
 * @since 2023-02-22 09:45:58
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    OrderitemVoMapper orderitemVoMapper;

    @Autowired
    CommentVoMapper commentVoMapper;

    @Override
    public List<Comment> queryComments(Integer shopId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", shopId);
        List<Comment> listCom = commentMapper.selectList(queryWrapper);
        return listCom;
    }



    @Override
    public Page<CommentVo> queryCommentsAndDetails(Integer currentPage,Integer pageSize,Integer shopId) {
        Page<Comment> cPage = new Page<>(currentPage,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);
        Page<Comment> commentPage = commentMapper.selectPage(cPage,queryWrapper);
        Page<CommentVo> commentVoPage = new Page<>();
        List<CommentVo> commentVoList = new ArrayList<>();

        for (Comment comment : commentPage.getRecords()) {
            List<OrderitemVo> orderitemVos = orderitemVoMapper.selectDetail(comment.getOrderId());
            String details = "";
            BigDecimal totalPrice = new BigDecimal(0.00);
            CommentVo commentVo = new CommentVo();
            for (OrderitemVo orderitemVo : orderitemVos) {
                if (orderitemVo.getOrderId() == comment.getOrderId()){
                    orderitemVo.getName();
                    Integer count = orderitemVo.getCount();
                    BigDecimal price = orderitemVo.getPrice();
                    Double priceDou = price.doubleValue();
                    BigDecimal sumPrice = BigDecimal.valueOf(count * priceDou);
                    totalPrice = totalPrice.add(sumPrice);
                    details = details + "  " + orderitemVo.getName() + "  " + count + "*" + price + "  小计：" + sumPrice;
                    commentVo.setOrderDetails(details);
                    commentVo.setTotalPrice(totalPrice);
                }else {
                    details="客户尚未评价，快邀请他来点评吧";
                    totalPrice=BigDecimal.ZERO;
                    commentVo.setOrderDetails(details);
                    commentVo.setTotalPrice(totalPrice);
                }

            }
            commentVo.setId(comment.getId());
            commentVo.setUserId(comment.getUserId());
            commentVo.setOrderId(comment.getOrderId());
            commentVo.setShopId(comment.getShopId());
            commentVo.setComments(comment.getComments());
            commentVo.setGrade(comment.getGrade());
            commentVo.setCreateTime(comment.getCreateTime());
            commentVo.setReply(comment.getReply());
            commentVo.setReplyTime(comment.getReplyTime());

            commentVoList.add(commentVo);
        }
        commentVoPage.setRecords(commentVoList);
        commentVoPage.setTotal(commentPage.getTotal());
        commentVoPage.setSize(commentPage.getSize());
        commentVoPage.setPages(commentPage.getPages());
        commentVoPage.setCurrent(commentPage.getCurrent());


        return commentVoPage;

    }

    @Override
    public int updatrToReply(Integer commentId, String reply) {
        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<>();
        Comment comment = new Comment();
        comment.setReply(reply);
        updateWrapper.eq("id", commentId);
        int update = commentMapper.update(comment, updateWrapper);

        return update;
    }

    @Override
    public Comment queryOne(Integer commentId) {
        return null;
    }
}
